| Conditions | 15 |
| Paths | 26 |
| Total Lines | 128 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Page.js ➔ ??? often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import Handlebars from 'handlebars' |
||
| 29 | constructor(templateId, template, json, onlyHTML = false) { |
||
| 30 | // HOOKS beforePageJson |
||
| 31 | json = Hooks.instance.trigger('beforePageJson', json) |
||
| 32 | |||
| 33 | if(typeof Handlebars.templates[templateId] !== 'undefined' && |
||
| 34 | Handlebars.templates[templateId] !== null && |
||
| 35 | config.files.templates.precompile |
||
| 36 | ){ |
||
| 37 | |||
| 38 | template = Handlebars.templates[templateId] |
||
| 39 | this.html = template(json, {data: {intl: config.intlData}}) |
||
| 40 | |||
| 41 | //console.log('precompile') |
||
| 42 | |||
| 43 | } else { |
||
| 44 | |||
| 45 | this._onlyHTML = onlyHTML |
||
| 46 | this.template = template |
||
| 47 | this.HbsTemplatePath = path.join(config.root, config.templates.url, 'hbs/'+templateId+'.hbs') |
||
| 48 | |||
| 49 | abeEngine.instance.content = json |
||
| 50 | |||
| 51 | // This pattern finds all abe tags which are not enclosed in a html tag attribute |
||
| 52 | // it finds this one: <title>{{abe type='text' key='meta_title' desc='Meta title' tab='Meta' order='4000'}}</title> |
||
| 53 | // it excludes this one: <meta name="description" content='{{abe type="text" key="meta_description" desc="Meta description" tab="Meta" order="4100"}}"/> |
||
| 54 | this.abePattern = /[^"']({{abe.*?type=[\'|\"][text|rich|textarea]+[\'|\"][\s\S].*?}})/g |
||
| 55 | |||
| 56 | // This pattern finds all abe tags enclosed in a HTML tag attribute |
||
| 57 | this.abeAsAttributePattern = /( [A-Za-z0-9\-\_]+=["|']{1}{{abe.*?}})/g |
||
| 58 | |||
| 59 | // This pattern finds all {{#each ...}}...{{/each}} blocks |
||
| 60 | this.eachBlockPattern = />\s*(\{\{#each (\r|\t|\n|.)*?\/each\}\})/g |
||
| 61 | |||
| 62 | // This pattern finds all {{#each ...}}...{{/each}} blocks |
||
| 63 | this.blockPattern = /(\{\{#each.*\}\}[\s\S]*?\{\{\/each\}\})/g |
||
| 64 | |||
| 65 | // Remove text with attribute "visible=false" |
||
| 66 | this._removeHidden() |
||
| 67 | |||
| 68 | if(!this._onlyHTML) { |
||
| 69 | |||
| 70 | // Surrounds each Abe tag (which are text/rich/textarea and not in html attribute) with <abe> tag |
||
| 71 | // ie. <title><abe>{{abe type='text' key='meta_title' desc='Meta title' tab='Meta' order='4000'}}</abe></title> |
||
| 72 | this._encloseAbeTag() |
||
| 73 | } |
||
| 74 | |||
| 75 | // je rajoute les index pour chaque bloc lié à un each |
||
| 76 | this._indexEachBlocks() |
||
| 77 | |||
| 78 | if(!this._onlyHTML){ |
||
| 79 | |||
| 80 | // Je maj les attributs associés aux Abe qui sont dans des attributs de tag HTML |
||
| 81 | this._updateAbeAsAttribute() |
||
| 82 | |||
| 83 | // je rajoute les attributs pour les tags Abe (qui ne sont pas dans un attribut HTML) |
||
| 84 | this._updateAbeAsTag() |
||
| 85 | |||
| 86 | // Don't know what it does... |
||
| 87 | var source = config.source.name |
||
| 88 | if(typeof json[source] !== 'undefined' && json[source] !== null) { |
||
| 89 | var keys = Object.keys(json[source]) |
||
| 90 | |||
| 91 | for(var i in keys) { |
||
| 92 | var replaceEach = new RegExp(`<!-- \\[\\[${keys[i]}\\]\\][\\s\\S]*?-->`, 'g') |
||
| 93 | this.template = this.template.replace(replaceEach, '') |
||
| 94 | |||
| 95 | var patAttrSource = new RegExp(' ([A-Za-z0-9\-\_]+)=["|\'].*?({{' + keys[i] + '}}).*?["|\']', 'g') |
||
| 96 | var patAttrSourceMatch = this.template.match(patAttrSource) |
||
| 97 | var patAttrSourceInside = null |
||
| 98 | var patAttrSourceCheck = null |
||
| 99 | var checkEscaped = null |
||
| 100 | |||
| 101 | if(patAttrSourceMatch != null) { |
||
| 102 | patAttrSourceInside = new RegExp('(\\S+)=["\']?((?:.(?!["\']?\\s+(?:\\S+)=|[>"\']))+.)["\']?({{' + keys[i] + '}}).*?["|\']', 'g') |
||
| 103 | Array.prototype.forEach.call(patAttrSourceMatch, (pat) => { |
||
| 104 | patAttrSourceCheck = patAttrSourceInside.exec(pat) |
||
| 105 | if(patAttrSourceCheck != null) { |
||
| 106 | var checkEscaped = /["|'](.*?)["|']/ |
||
| 107 | checkEscaped = checkEscaped.exec(patAttrSourceCheck[0]) |
||
| 108 | if(checkEscaped != null && checkEscaped.length > 0) { |
||
| 109 | checkEscaped = escape(checkEscaped[1]) |
||
| 110 | this.template = this.template.replace( |
||
| 111 | patAttrSourceCheck[0], |
||
| 112 | ` data-abe-attr="${patAttrSourceCheck[1]}" data-abe-attr-escaped="${checkEscaped}" data-abe="${keys[i]}" ${patAttrSourceCheck[0]}` |
||
| 113 | ) |
||
| 114 | } |
||
| 115 | } |
||
| 116 | }) |
||
| 117 | } |
||
| 118 | |||
| 119 | var eachSource = new RegExp(`({{#each ${keys[i]}}[\\s\\S a-z]*?{{\/each}})`, 'g') |
||
| 120 | var matches = this.template.match(eachSource) |
||
| 121 | if(typeof matches !== 'undefined' && matches !== null) { |
||
| 122 | Array.prototype.forEach.call(matches, (match) => { |
||
| 123 | this.template = this.template.replace(match, `${match}<!-- [[${keys[i]}]] ${cmsTemplates.encodeAbeTagAsComment(match)} -->`) |
||
| 124 | }) |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | this._addSource(json) |
||
| 131 | |||
| 132 | // We remove the {{abe type=data ...}} from the text |
||
| 133 | this.template = cmsData.source.removeDataList(this.template) |
||
| 134 | |||
| 135 | // It's time to replace the [index] by {{@index}} (concerning each blocks) |
||
| 136 | this.template = this.template.replace(/\[index\]\./g, '{{@index}}-') |
||
| 137 | |||
| 138 | if(config.files.templates.precompile){ |
||
| 139 | // Let's persist the precompiled template for future use (kind of cache) |
||
| 140 | fse.writeFileSync(this.HbsTemplatePath, Handlebars.precompile(this.template), 'utf8') |
||
| 141 | Manager.instance.addHbsTemplate(templateId) |
||
| 142 | } |
||
| 143 | |||
| 144 | // I compile the text |
||
| 145 | var compiledTemplate = Handlebars.compile((!this._onlyHTML) ? cmsTemplates.insertDebugtoolUtilities(this.template) : this.template) |
||
| 146 | |||
| 147 | // I create the html page ! yeah !!! |
||
| 148 | this.html = compiledTemplate(json, {data: {intl: config.intlData}}) |
||
| 149 | } |
||
| 150 | |||
| 151 | if(this._onlyHTML) { |
||
| 152 | this.html = Hooks.instance.trigger('afterPageSaveCompile', this.html, json) |
||
| 153 | }else { |
||
| 154 | this.html = Hooks.instance.trigger('afterPageEditorCompile', this.html, json) |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 309 | } |